package com.javasm.service.goods.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.goods.GoodsCriteria;
import com.javasm.domin.entity.goods.Gifts;
import com.javasm.domin.entity.goods.Goods;
import com.javasm.domin.vo.goods.GiftsVo;
import com.javasm.domin.vo.goods.GoodsVO;
import com.javasm.mapper.goods.GoodsMapper;
import com.javasm.service.base.impl.BaseServiceimpl;
import com.javasm.service.goods.IGoodsService;
import com.javasm.transfer.goods.GoodsTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author whl
 * @since 2022-01-16
 */
@Service
@Transactional
@RequiredArgsConstructor
public class GoodsServiceImpl extends BaseServiceimpl<Goods> implements IGoodsService {

    private final GoodsMapper goodsMapper;
    private final GoodsTransfer goodsTransfer;

    @Override
    public GoodsVO getvoById(Long id) {
        Goods goods=goodsMapper.selectById(id);
        GoodsVO goodsVO=goodsTransfer.set(goods);
        return goodsVO;
    }

    @Override
    public PageResult<GoodsVO> searchPage(GoodsCriteria goodsCriteria) {
        PageHelper.startPage(goodsCriteria.getCurrentPage(), goodsCriteria.getPageSize());
        LambdaQueryWrapper<Goods> lambda = new QueryWrapper<Goods>().lambda();

        if (!StringUtils.isEmpty(goodsCriteria.getBrandId())) {
            lambda.like(Goods::getBrandId, goodsCriteria.getBrandId());
        }
        if (!StringUtils.isEmpty(goodsCriteria.getGoodsName())) {
            lambda.like(Goods::getGoodsName, goodsCriteria.getGoodsName());
        }
        if (!StringUtils.isEmpty(goodsCriteria.getProductId())) {
            lambda.eq(Goods::getProductId, goodsCriteria.getProductId());
        }
        if (!StringUtils.isEmpty(goodsCriteria.getProductColorId())) {
            lambda.eq(Goods::getProductColorId, goodsCriteria.getProductColorId());
        }
        if (!StringUtils.isEmpty(goodsCriteria.getTypeId())) {
            lambda.eq(Goods::getTypeId, goodsCriteria.getTypeId());
        }
        List<Goods> goods = goodsMapper.selectList(lambda);
        PageInfo<Goods> pageInfo = new PageInfo<>(goods);
        List<GoodsVO> goodsVOS = goodsTransfer.setInfo(goods);

        return new PageResult<GoodsVO>(pageInfo.getTotal(), goodsVOS);
    }
}
